/**
* 头部菜单
*/ 
<template>
  <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
    <el-button class="buttonimg">
      <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
    </el-button>
    
    <el-submenu index="2" class="submenu">
      <template slot="title" v-if="user.rid == 1" >超级管理员</template>
      <template slot="title" v-if="user.rid == 2" >学员用户</template>
      <template slot="title" v-if="user.rid == 3" >教练用户</template>
      <el-menu-item style="color:red">{{user.username}}</el-menu-item>
      <el-menu-item @click="reload()" >刷新</el-menu-item>
      <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
    </el-submenu>
    <el-menu-item class="submenu">
        <el-avatar :size="50" :src="user.url"></el-avatar>
    </el-menu-item>
  </el-menu>
</template>
<script>
export default {
  name: 'navcon',
  data() {
    return {
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      user: {}
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
      this.user = JSON.parse(window.sessionStorage.getItem("user"));
  },
  methods: {
    reload(){
        this.findUser();
    },
    findUser(){
           this.$api.get("/user/token/findUserByid",'',Response=>{
              
                     if(Response.data.code == 200){
                          this.user = Response.data.data;
                          window.sessionStorage.setItem("user",JSON.stringify(Response.data.data));
                          window.location.reload();
                     }
           })
       },
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          setTimeout(() => {
            window.sessionStorage.clear();
            this.$router.replace({ path: '/' })
            this.$message({
              type: 'success',
              message: '已退出登录!'
            })
          }, 1000)
         
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    }
  }
}
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
</style>